#ifndef LIB_STRING_UTIL_HPP
#define LIB_STRING_UTIL_HPP

#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

bool startsWith(const std::string& str, const std::string& prefix) {
  if (str.length() < prefix.length()) {
    return false;
  }
  return str.compare(0, prefix.length(), prefix) == 0;
}

bool endsWith(const std::string& str, const std::string& suffix) {
  if (str.length() < suffix.length()) {
    return false;
  }
  return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0;
}
std::vector<std::string> stringSplit(const std::string &s, char c) {
  std::stringstream ss(s);
  std::string piece;
  std::vector<std::string> result;
  while (std::getline(ss, piece, c)) {
    result.push_back(piece);
  }
  return result;
}

std::string& stringStrip(std::string &s) {
  if (s.empty()) return s;

  s.erase(0, s.find_first_not_of(" "));
  s.erase(s.find_last_not_of(" ") + 1);
  return s;
}

void upper(std::string& str) {
  std::transform(str.begin(), str::end(), str.begin(), ::toupper);
}

std::vector<std::string> readLines(const std::string& filename) {
  std::ifstream istrm(filename);
  if (!istrm.is_open()) {
    std::cout << "failed to open " << filename << std::endl;
    return {};
  }
  std::vector<std::string> result;
  std::string str;
  while (getline(istrm, str)) {
    result.push_back(str);
    std::cout << str << std::endl;
  }
  return result;
}

#endif
